/**
 * 
 */
package cn.egame.identify.app.extraction.dao;

import java.util.List;

import org.apache.log4j.Logger;

import cn.egame.common.data.BaseDao;
import cn.egame.common.data.SqlUtils;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.identify.app.extraction.pojo.IdentifyApp;


/**
 * Description  TODO
 * 
 * @ClassName IdentifyAppDao
 *
 * @Copyright 炫彩互动
 * 
 * @Project egame.mobile.extraction
 * 
 * @Author zhangjb
 * 
 * @Create Date  2014年3月7日
 * 
 * @Modified by none
 *
 * @Modified Date 
 */
public class IdentifyAppDao extends BaseDao {

    private Logger log = Logger.getLogger(IdentifyAppDao.class);
    
    public IdentifyAppDao() throws ExceptionCommonBase {
        super("egame_game");
    }

    /**
     * @Description: 通过包名取得应用的数量
     * @param packetName 所给的包名    
     * @return 所得的记录数
     */
    public int getAppByPacketName(String packetName) throws ExceptionCommonBase {
        String sql = "select Count(*) from t_game_file where package_name = " + "'" + packetName + "'";
        return this.getInt(sql);
    }
    
    /**
     * @Description: 得到所有游戏文件表中的包名
     * @return 所得的记录集合
     */
    public List<String> getPackageName() throws ExceptionCommonBase {
        String sql = "select distinct package_name from t_game_file";
        return this.getListString(sql);
    }
    
    /**@Description: 更新或者插入未识别的应用
     * @param identifyApp 所給的应用
     * @return 更新的数量
     */
    public int setIdentifyApp(IdentifyApp identifyApp) throws ExceptionCommonBase {
        StringBuilder sb = new StringBuilder();
        long now = System.currentTimeMillis();
        sb.append("insert into t_identify_app (id, name, packet_name, version_name, version_code, run_count, run_time, install, type, status, insert_time, update_time) ");
        sb.append("values(%1$d,%2$s,%3$s,%4$s,%5$d,%6$d,%7$d,%8$d,%9$d,%10$d,%11$d,%12$d) ");
        sb.append(" on duplicate key update name=%2$s,version_code=%5$d,install=install+%8$d,update_time=%12$d");
        
        String sql = String.format(sb.toString(), identifyApp.getId(),SqlUtils.QuataStr(identifyApp.getName()),
                SqlUtils.QuataStr(identifyApp.getPacketName()),SqlUtils.QuataStr(identifyApp.getVersionName()),
                identifyApp.getVersionCode(),identifyApp.getRunCount(),identifyApp.getRunTime(),identifyApp.getInstall(),
                identifyApp.getType(),identifyApp.getStatus(),now,now);
        
        return this.getIdentityId(sql);
    }
}
